package org.example.school.controller;

import org.example.school.business.StudentInfo;
import org.example.school.business.ParentInfo;
import org.example.school.business.TeacherInfo;
import org.example.school.common.Context;
import org.example.school.common.Result;
import org.example.school.entity.AttendanceRecords;
import org.example.school.entity.Leave;
import org.example.school.entity.Users;
import org.example.school.model.dto.GuardianProfileDTO;
import org.example.school.model.dto.LeaveDTO;
import org.example.school.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 考勤打卡控制器
 */
@RestController
@RequestMapping("/attendance")
@CrossOrigin
public class AttendanceController {

    @Autowired
    private StudentInfo studentInfo;
    
    @Autowired
    private ParentInfo parentInfo;
    
    @Autowired
    private TeacherInfo teacherInfo;
    
    @Autowired
    private IUsersService usersService;
    
    @Autowired
    private IParentsService parentsService;

    /**
     * 早上签到接口
     * @return 签到结果
     */
    @PostMapping("/clock-in-morning")
    public Result<String> clockInMorning(@RequestParam String studentNumber) {
        try {
            String result = studentInfo.clockInMorning(studentNumber);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("签到失败: " + e.getMessage());
        }
    }

    /**
     * 下午签退接口
     * @return 签退结果
     */
    @PostMapping("/clock-out-afternoon")
    public Result<String> clockOutAfternoon(@RequestParam String studentNumber) {
        try {
            String result = studentInfo.clockOutAfternoon(studentNumber);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("签退失败: " + e.getMessage());
        }
    }
    
    /**
     * 学生请假接口
     * @return 请假结果
     */
    @PostMapping("/leave")
    public Result<String> leave(@RequestBody LeaveDTO leaveDTO) {
        try {
            String result = studentInfo.leave(leaveDTO);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("请假失败: " + e.getMessage());
        }
    }
    
    //获取请假记录
    @GetMapping("/leaveStatus")
    public Result<List<Leave>> getLeaveStatus(@RequestParam String studentNumber) {
        try {
            List<Leave> list = studentInfo.getLeaveStatus(studentNumber);
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    //撤销请假
    @PostMapping("/cancelLeave")
    public Result<String> cancelLeave(@RequestParam long leaveId) {
        try {
            String result = studentInfo.cancelLeave(leaveId);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("撤销失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询监护人学生接口
     * @return 孩子信息列表
     */
    @GetMapping("/parentProfile")
    public Result<List<GuardianProfileDTO.ChildInfo>> getParentChildren() {
        try {
            Long userId = Context.getId();
            Users currentUser = usersService.getById(userId);
            String userType = currentUser.getUserType();
            if (!"3".equals(userType)) { // 监护人
                return Result.error("您没有权限查看该学生的考勤信息");
            }

            //获取监护人信息
            GuardianProfileDTO parentProfile = parentsService.getGuardianProfile(userId);
            return Result.success(parentProfile.getChildren());
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    // 查询学生考勤记录接口
    @GetMapping("/record")
    public Result<List<AttendanceRecords>> getAttendRecords(@RequestParam String studentNumber){
        try {
            // 根据用户类型调用不同的方法
            Long userId = Context.getId();
            Users currentUser = usersService.getById(userId);
            String userType = currentUser.getUserType();
            
            List<AttendanceRecords> records;
            if ("2".equals(userType)) { // 学生
                records = studentInfo.getAttendRecords(studentNumber);
            } else if ("3".equals(userType)) { // 家长
                records = parentInfo.getChildAttendRecords(studentNumber);
            } else {
                return Result.error("您没有权限查看该学生的考勤信息");
            }
            
            return Result.success(records);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}